package com.example.huixingzheng.adapter;

/**
 * 功能：
 * 作者：
 * 日期：2025年03月17日
 */

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.huixingzheng.R;
import com.example.huixingzheng.bean.DataModel;
import com.example.huixingzheng.bean.Device;

import java.util.List;

public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder> {

    private List<Device> deviceList;

    // 构造方法
    public DeviceAdapter(List<DataModel> deviceList) {
        this.deviceList = deviceList;
    }

    // 创建ViewHolder
    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_device, parent, false);
        return new DeviceViewHolder(view);
    }

    // 绑定数据
    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, int position) {
        Device device = deviceList.get(position);
        holder.tvDeviceName.setText(device.getName());
        holder.tvTemperature.setText(device.getTemperature() + "℃");
        holder.tvHumidity.setText("湿度: " + device.getHumidity() + "%");
        holder.tvLight.setText("光照强度: " + device.getLight());
        holder.tvSmoke.setText("烟雾浓度: " + device.getSmoke());
    }

    @Override
    public int getItemCount() {
        return deviceList == null ? 0 : deviceList.size();
    }

    // 自定义ViewHolder
    public static class DeviceViewHolder extends RecyclerView.ViewHolder {
        TextView tvDeviceName;
        TextView tvTemperature;
        TextView tvHumidity;
        TextView tvLight;
        TextView tvSmoke;

        public DeviceViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_device_name);
            tvTemperature = itemView.findViewById(R.id.tv_temperature);
            tvHumidity = itemView.findViewById(R.id.tv_humidity);
            tvLight = itemView.findViewById(R.id.tv_light);
            tvSmoke = itemView.findViewById(R.id.tv_smoke);
        }
    }
}
